package menuDao

import (
	"gitee.com/yangguoliang001/go-gin-vue3/QO"
	"gitee.com/yangguoliang001/go-gin-vue3/model"
)

func (m *menuDao) QueryMenuList() (menuList []model.MenuDO, err error) {
	err = m.db.Debug().Table("menu").Where("is_deleted = ?", -1).Find(&menuList).Error
	return
}

func (m *menuDao) QueryMenuUsedList() (menuList []model.MenuDO, err error) {
	err = m.db.Debug().Table("menu").Where("is_deleted = ? AND is_used = ?", -1, 1).Find(&menuList).Error
	return
}

func (m *menuDao) QueryMenuItemDetail(id string) (menuItem model.MenuDO, err error) {
	err = m.db.Debug().Table("menu").Where("id = ?", id).First(&menuItem).Error
	return
}

func (m *menuDao) EditMenu(editReq QO.EditReq) (err error) {
	if editReq.Id == -1 {
		// 新增
		item := &QO.EditReq{
			Pid:   editReq.Pid,
			Name:  editReq.Name,
			Path:  editReq.Path,
			Icon:  editReq.Icon,
			Level: editReq.Level,
		}
		err = m.db.Debug().Table("menu").Create(&item).Error
		return
	}
	err = m.db.Debug().Table("menu").Where("id = ?", editReq.Id).Updates(&editReq).Error
	return
}

func (m *menuDao) UpdateMenuUsed(usedReq QO.MenuUsedReq) (err error) {
	return m.db.Debug().Table("menu").Where("id = ?", usedReq.Id).Update("is_used", usedReq.Used).Error
}

func (m *menuDao) UpdateMenuSort(sortReq QO.MenuSortReq) (err error) {
	return m.db.Debug().Table("menu").Where("id = ?", sortReq.Id).Update("sort", sortReq.Sort).Error
}

func (m *menuDao) DeleteMenu(id int32) (err error) {
	return m.db.Debug().Table("menu").Where("id = ?", id).Update("is_deleted", 1).Error
}
